<?php
/**
 * ClassName:数据填充器
 * @auditor:leijunxiang
 * @time 2025/4/9 10:08
 */

namespace app\tool\word;

use app\tool\ToolBase;
use think\facade\Log;

class DataFillHandle extends ToolBase
{
    //标题定位对象
    protected $titlePositionObj;
    //excel对象
    protected $excelObj;
    //数据定位对象
    protected $dataPositionObj;
    //表格数据对象
    protected $tableDataObj;
    //开始标识
    protected $startCoord;


    /**
     * (true/false)开始填充(标题定位对象,数据定位对象,表格数据对象,excel对象)
     */
    public function startFill($titlePositionObj, $dataPositionObj, $tableDataObj, $excelObj, $row, $col)
    {
        if ($titlePositionObj && $dataPositionObj && $tableDataObj && $excelObj && $row && $col) {
            //初始化数据
            $this->initData($titlePositionObj, $dataPositionObj, $tableDataObj, $excelObj);
            //校验开始标识
            $this->verifyStartCoord($row, $col);
            //开始填充
            $this->startFillData();
            //清楚数据
            $this->cleanData();
        } else {
            $this->errCode = FAIL;
            $this->errMsg = "数据未初始化";
        }
        return $this->isGoOn();
    }

    /**
     * 初始化数据
     */
    protected function initData($titlePositionObj, $dataPositionObj, $tableDataObj, $excelObj)
    {
        $this->init();
        //标题定位对象
        $this->titlePositionObj = $titlePositionObj;
        //excel对象
        $this->excelObj = $excelObj;
        //数据定位对象
        $this->dataPositionObj = $dataPositionObj;
        //表格数据对象
        $this->tableDataObj = $tableDataObj;
    }

    /**
     * 校验开始标识(行,列)
     */
    protected function verifyStartCoord($row, $col)
    {
        if (!$this->isGoOn()) {
            return;
        }
        $tool = app(WordHandleTool::class);
        //(字符串)开始处理(excel对象,行,列)
        $value = $tool->startHandle($this->excelObj, $row, $col);
        $startFlag = $this->tableDataObj->getStartFlag();
        if ($value !== $startFlag) {
            $this->errMsg = "还未找到开始标识，尝试继续查找";
            $this->errCode = WAIT;
            return;
        }else{
            $this->errCode = SUCCESS;
            $this->errMsg = "找到标识,开始执行";
        }
        $arr = [$row, $col];
        $this->startCoord = $arr;
    }

    /**
     * 开始填充
     */
    protected function startFillData()
    {
        if (!$this->isGoOn()) {
            return;
        }

        //渠道填充
        $this->channelFill();
        //区域填充
        $this->areaFill();
        //重量填充
        $this->weightFill();
        //数据填充
        $this->dataFill();
    }

    /**
     * 清楚数据
     */
    protected function cleanData()
    {
        //标题定位对象
        $this->titlePositionObj = null;
        //excel对象
        $this->excelObj = null;
        //数据定位对象
        $this->dataPositionObj = null;
        //表格数据对象
        $this->tableDataObj = null;
        //开始标识
        $this->startCoord = null;
    }

    /**
     * 渠道填充
     */
    protected function channelFill()
    {
        //获取渠道名称坐标
        $arr = [$this->startCoord[0] - 1, $this->startCoord[1]];
        $this->titlePositionObj->setChannelCoord($arr);
    }

    /**
     * 区域填充
     */
    protected function areaFill()
    {
        //获取区域名称坐标
        $this->titlePositionObj->setAreaCoord($this->startCoord);
    }

    /**
     * 重量填充
     */
    protected function weightFill()
    {
        //列+1
        $col = chr(ord($this->startCoord[1]) + 1);
        //重量开始坐标
        $start = [$this->startCoord[0],$col];
        $this->titlePositionObj->setWeightStartCoord($start);
        //重量结束坐标
        $rowOffset = $this->tableDataObj->getRowOffset();
        //获取列
        $col = chr(ord($col) + ($rowOffset - 1));
        $end = [$this->startCoord[0], $col];
        $this->titlePositionObj->setWeightEndCoord($end);
    }

    /**
     * 数据填充
     */
    protected function dataFill()
    {
        //左上角坐标
        $leftTop = [$this->startCoord[0] + 1, $this->startCoord[1]];
        //获取右下角坐标
        $rightBottomCoord = $this->getRightBottomCoord();
        //保存左上角坐标和右下角坐标
        $this->dataPositionObj->setLeftTopCoord($leftTop);
        $this->dataPositionObj->setRightTopCoord($rightBottomCoord);
    }

    /**
     * 获取右下角坐标
     */
    protected function getRightBottomCoord()
    {
        $flag = [];
        //右下角列
        $col = $this->titlePositionObj->getWeightEndCoord()[1];
        //行
        $row = $this->getRow($this->startCoord[0] + 1, $this->startCoord[1]);
        $flag = [$row, $col];
        return $flag;
    }

    /**
     * 获取右下角行(当前左上角区域数据行，当前左上角区域数据列)
     * @return void
     */
    protected function getRow($row, $col)
    {
        $flag = "";
        while (true) {
            $tool = app(WordHandleTool::class);
            //(字符串)开始处理(excel对象,行,列)
            $firstValue = $tool->startHandle($this->excelObj, $row, $col);
//            $firstValue = $this->excelObj->getCell($col . $row)->getValue();
            if (empty($firstValue)) {
                //如果为空，结束
                $flag = $row-1;
                break;
            }
            //获取右侧数据
            //(字符串)开始处理(excel对象,行,列)
            $rightValue = $tool->startHandle($this->excelObj, $row, chr(ord($col) + 1));
//            $rightValue = $this->excelObj->getCell(chr(ord($col) + 1) . $row)->getValue();
            if (empty($rightValue)&&$rightValue!=0) {
                $flag = $row-1;
                break;
            }
            $row++;
        }
        return $flag;
    }


    /**
     * (行坐标)读取下一个开始标识(当前行，当前列)
     */
    protected function readNextStartCoord($row, $col)
    {
        $flag = "";
        $tool = app(WordHandleTool::class);
        for ($i = $row; true; $i++) {
            //查找纵向开始标识
            //(字符串)开始处理(excel对象,行,列)
            $value = $tool->startHandle($this->excelObj, ($row + $i), $col);
//            $value = $this->excelObj->getCell($col . ($row + $i))->getValue();
            //开始标识
            $startFlag = $this->tableDataObj->getStartFlag();
            if ($value == $startFlag) {
                $this->titlePositionObj->setNextStartCoord([$col . ($row + $i)]);
                $flag = $row + $i;
                break;
            } else {
                //当前行的下5行数据都为空，就跳出循环
            }
        }
        return $flag;
    }


}